/**  
 * All rights Reserved, Designed By www.tydic.com
 * @Title:  SchedulerServiceImpl.java   
 * @Package com.axin.service.impl   
 * @Description:    TODO(用一句话描述该文件做什么)   
 * @author: axin     
 * @date:   2019年4月19日 下午10:44:54   
 * @version V1.0 
 * @Copyright: 2019 www.hao456.top Inc. All rights reserved. 
 */
package com.axin.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.axin.common.entity.ApiToken;
import com.axin.common.exception.AccountLockException;
import com.axin.common.exception.ApiException;
import com.axin.common.exception.TaskStartException;
import com.axin.common.quartz.QuartzManager;
import com.axin.entity.HbSymbols;
import com.axin.entity.HbTask;
import com.axin.job.JobB;
import com.axin.service.AccountService;
import com.axin.service.CommonService;
import com.axin.service.MessageService;
import com.axin.service.OrderService;
import com.axin.service.SchedulerService;
import com.axin.service.TaskService;

/**   
 * @Description: TODO 
 * @ClassName:  SchedulerServiceImpl
 * @author:  Axin 
 * @date:   2019年4月19日 下午10:44:54   
 * @Copyright: 2019 www.hao456.top Inc. All rights reserved. 
 */
@Service
public class SchedulerServiceImpl implements SchedulerService {

	@Autowired
	private AccountService accountService;
	@Autowired
	private OrderService orderService;
	@Autowired
	private CommonService commonService;
	@Autowired
	private TaskService taskService;
	
	@Autowired
	private QuartzManager quartzManager;
	@Autowired
	private MessageService messageService;
	
	private Logger logger = LoggerFactory.getLogger(SchedulerServiceImpl.class);
	
	/* (non-Javadoc)
	 * @see com.axin.service.SchedulerService#startTask(com.axin.common.entity.ApiToken, java.lang.String)
	 */
	@Override
	public void startTaskById(ApiToken token,Long taskId, Long userId) 
				throws ApiException, AccountLockException, Exception {
		HbTask task = this.taskService.getTaskById(taskId);
		if(task == null){
			throw new TaskStartException("任务不存在，或已被关闭，任务启动失败。");
		}
		
		String symbol = task.getSymbol();//交易对
		int buyTradeCount = task.getBuyTradeCount();//本轮第几单
		int buyTradeLimit =task.getBuyTradeLimit();//系统自动买入单数上限
		String firstMoney = task.getFirstMoney();//首单金额（计价币）
		
		String buyLevel = task.getBuyLevel();//档位：1-50% 2-40% 3-30% 4-20% 5-10%
		String buyMethod = task.getBuyMethod(); //1-斐波 2-倍投 3-立方倍投
		String reboundPercent = task.getReboundPercent();//反弹百分比
		
		String buyBasePrice = task.getBuyBasePrice();
		
		String profitPercent =task.getProfitPercent(); //2.5%-5%止盈百分比 最大10%
		String fallBackPercent = task.getFallBackPercent();//回落百分比
		
		Integer loopCount = task.getLoopCount();
		String totalAmount = task.getTotalAmount();//仓位数量
		String totalMoney = task.getTotalMoney();//购买总金额
		//账户id
		Integer accountId = this.accountService.getAccountsByType(token, AccountService.TYPE_SPOT);
		
		HbSymbols hbSymbol = this.commonService.selectLocalSymbolsBySymbol(symbol);
		int moneyScale = hbSymbol.getPricePrecision();
		int amountScale = hbSymbol.getAmountPrecision();
		String quoteCurrency = hbSymbol.getQuoteCurrency();
		String baseCurrency = hbSymbol.getBaseCurrency();
		
		Map<String,Object> params = new HashMap<>();
    	params.put("userId",userId);
    	params.put("token", token);
    	
    	params.put("symbol", symbol);
    	params.put("buyTradeCount", buyTradeCount);
    	params.put("buyTradeLimit", buyTradeLimit);
    	params.put("firstMoney", firstMoney);
    	
    	params.put("buyLevel", buyLevel);
    	params.put("buyMethod", buyMethod);
    	params.put("reboundPercent", reboundPercent);
    	params.put("minPrice", "0");//最低价初始为0
    	params.put("downing", false);//下跌中初始化为false
    	params.put("buyBasePrice",buyBasePrice);
    	
    	params.put("profitPercent", profitPercent);
    	params.put("fallBackPercent", fallBackPercent);
    	params.put("maxPrice", "0");//最高价初始为0
    	params.put("uping", false);//上涨中初始化为false
    	
    	
    	params.put("loopCount", loopCount);
    	params.put("totalAmount", totalAmount);
    	params.put("totalMoney", totalMoney);
    	
    	params.put("accountId", accountId);
    	params.put("moneyScale", moneyScale);
    	params.put("amountScale", amountScale);
    	params.put("quoteCurrency", quoteCurrency);
    	params.put("baseCurrency", baseCurrency);
		//启动任务
		quartzManager.addJobForInterval(taskId+"", taskId+"", taskId+"", taskId+"", params,500,JobB.class);    
		
		this.messageService.sendConsoleMessage(userId, "任务将在5秒钟后开始运行。");
		HbTask hbTask = new HbTask(taskId,"1");
		this.taskService.updateTaskById(hbTask, userId);
		
		

	}

}
